<template>
  <div class="row justify-content-center align-items-center">
    <!-- 查询组 -->
    <div class="col">
      <div style="float: right">
        <div class="inline-block align-middle">
          <el-button
            :id="activeId == '1' ? 'active-button' : ''"
            @click="routeLink('Focus', '1')"
            class="button align-middle hot"
            type="text"
          >
            <strong>关注</strong>
          </el-button>

          <el-button
            :id="activeId == '2' ? 'active-button' : ''"
            class="button align-middle"
            @click="routeLink('Recommend', '2')"
            type="text"
          >
            <strong ref="recommend" style="color: red"
              ><i class="iconfont icon-redian"></i>推荐</strong
            >
          </el-button>

          <el-button
            @click="routeLink('Message', '3')"
            :id="activeId == 3 ? 'active-button' : ''"
            class="button align-middle"
            type="text"
          >
            <strong>消息</strong>
          </el-button>
        </div>

        <div class="align-middle" style="display: inline-block">
          <el-input
            prefix-icon="el-icon-search"
            class="input d-none d-md-block"
            v-model="input"
            placeholder="查找内容..."
            clearable
          >
          </el-input>
        </div>
        <el-divider
          class="d-none d-sm-inline"
          direction="vertical"
        ></el-divider>

        <!-- 未登录 -->
        <div v-if="!isLogin" ref="avatar" class="inline-block">
          <span
            @click="
              dialog = true;
              login = true;
            "
            id="login"
            >登录
          </span>
          <span
            @click="
              dialog = true;
              login = false;
            "
            class="d-none d-sm-inline"
            id="login"
          >
            注册</span
          >
        </div>

        <!-- 头像 -->
        <div v-if="isLogin" class="inline-block align-middle">
          <el-avatar
            class="d-none d-sm-block"
            :size="40"
            :src="avatar"
          ></el-avatar>
        </div>

        <!-- 用户信息组 -->
        <div v-if="isLogin" class="inline-block align-middle">
          <strong style="cursor: pointer" @click="self()">{{
            username
          }}</strong>
          <el-tooltip effect="dark" content="管理员" placement="right">
            <i
              v-if="isAdmin"
              alt="管理员"
              style="color: gold; font-size: 15px; font-weight: bold"
              class="iconfont icon-renzheng"
            ></i>
          </el-tooltip>
          <br />
          <span @click="logout()" id="span">
            退出登录<i
              style="font-weight: bold"
              class="el-icon-switch-button"
            ></i
          ></span>
        </div>

        <el-dialog
          modal-append-to-body
          append-to-body
          title="登录与注册"
          :visible.sync="dialog"
          width="75%"
        >
          <div class="container-fluid">
            <div class="row">
              <div
                class="
                  col-12 col-md-8
                  offset-md-2
                  col-lg-6
                  offset-lg-3
                  col-xl-4
                  offset-xl-4
                "
              >
                <div class="row">
                  <div class="col-12" style="text-align: center">
                    <div class="inline-block">
                      <el-image :src="require(`logo`)"></el-image>
                    </div>
                  </div>

                  <div class="col-12" style="text-align: center">
                    <span
                      @click="login = true"
                      :class="login ? 'active' : null"
                      style="font-size: 18px; cursor: pointer"
                      >登录</span
                    >
                    <el-divider direction="vertical"></el-divider>
                    <span
                      @click="login = false"
                      :class="!login ? 'active' : null"
                      style="font-size: 18px; cursor: pointer"
                      >注册</span
                    >
                  </div>

                  <div class="col-12">
                    <login @success="success" v-if="login"></login>
                    <register @success="login = true" v-else></register>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { fake } from "../../assets/fake";
import Login from "../../components/login/Login.vue";
import Register from "../../components/register/Register.vue";
export default {
  components: { Login, Register },
  data() {
    return {
      input: "",
      isLogin: false,
      active: "recommend",
      dialog: false,
      login: false,
      username: "",
      avatar: require(`logo`),
      isAdmin: false,
      activeId: "2",
    };
  },
  methods: {
    logout() {
      fake.self = null;
      this.isLogin = false;
      this.isAdmin = false;
    },
    routeLink(name, id) {
      if (fake.self !== null || id === '2') {
        this.$router.push({
          name: name,
        });
        this.activeId = id;
      } else {
        this.$alert("登陆后才能查看哦", "提示", {
          confirmButtonText: "确定",
          type: "warning",
        });
      }
    },
    self() {
      let _this = this;
      this.$router.push({
        name: "UserInfo",
        query: {
          id: fake.self.info.id,
          type:'1'
        },
      });
    },
    success() {
      this.isLogin = true;
      this.username = fake.self.info.nick;
      this.avatar = fake.self.info.avatar;
      this.dialog = false;
      this.isAdmin = fake.self.isAdmin;
    },
  },
  mounted() {
    this.isLogin = !fake.self === null;
    if (fake.self !== null) {
      this.username = fake.self.info.nick;
      this.avatar = fake.self.info.avatar;
      this.isAdmin = fake.self.isAdmin;
    }
  },
  // beforeMount() {
  //   this.isAdmin = fake.self !== null && fake.self.isAdmin;
  //   if (fake.self !== null) {
  //     console.log(fake.self.isAdmin);
  //   }
  // },
};
</script>

<style scoped>
.inline-block {
  display: inline-block;
}
strong {
  font-size: 17px;
}
a {
  color: rgba(0, 0, 0, 0.678);
}
#span {
  font-size: 10px;
  color: red;
  transition: all 0.2s;
}
#span:hover {
  cursor: pointer;
  color: rgba(255, 0, 0, 0.647);
  transition: all 0.2s;
}
#login {
  cursor: pointer;
  font-size: 15px;
  font-weight: bold;
  color: rgb(0, 0, 0);
}
.button {
  color: #41b88393;
  margin-right: 25px;
  border: none;
}
.el-button--text:active {
  color: #41b88393;
}
.el-button--text:focus {
  color: #41b88393;
}
.el-button--text:hover {
  color: #41b883;
}
#active-button {
  color: #41b883;
  border-width: 0 0 5px 0;
  border-color: #41b883;
  border-style: solid;
}
.el-image {
  display: inline-block;
  width: 40px;
  height: 100%;
}
</style>